package com.vainycos.lotterysearch.schedule;

import com.vainycos.lotterysearch.config.ScheduledTaskCenter;
import com.vainycos.lotterysearch.model.WorldCupDetailDO;
import com.vainycos.lotterysearch.service.ClassicQuotService;
import com.vainycos.lotterysearch.service.WorldCupDetailService;
import com.vainycos.lotterysearch.service.WorldcupService;
import com.vainycos.lotterysearch.service.notify.DingNotify;
import com.vainycos.lotterysearch.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/22 14:37
 */
@Slf4j
@Component
public class WorldcupSyncData {

    @Autowired
    private WorldcupService worldcupService;

    @Autowired
    private WorldCupDetailService worldCupDetailService;

    @Autowired
    private ClassicQuotService classicQuotService;

    @Autowired
    private DingNotify dingNotify;

    @Autowired
    private ScheduledTaskCenter scheduledTaskCenter;

    /**
     * 早上9点钟，晚上11点的昨日比赛概况以及今日/明日比赛预告
     */
    @Scheduled(cron = "0 0 9,23 * * *")
    public void worldCupMatchOverview(){
        // 更新并输出报告结果
        worldcupService.initOverview();
        worldCupDetailService.initDetail();
        log.info("更新比赛成功");
        StringBuffer stringBuffer = new StringBuffer();
        // 获取昨日比赛
        List<WorldCupDetailDO> yesterdayDetails = worldCupDetailService.getDetailListByTime(DateUtil.getYesterday());
        stringBuffer.append("> " + classicQuotService.getOneByType(1).getContent() + " \n");
        stringBuffer.append("# 昨日比赛回顾" + " \n");
        int yesterdayCount = yesterdayDetails.size();
        stringBuffer.append("昨天有" + yesterdayCount + "场比赛" + "\n");
        yesterdayDetails.forEach(e->{
            stringBuffer.append(e.getMatchName() + " " + e.getStartTime() + " \n");
            stringBuffer.append(e.getLeftName() + "-" + e.getRightName() + " ");
            stringBuffer.append(e.getLeftScore() + ":" + e.getRightScore() + " \n");
            stringBuffer.append("关注热度：" + e.getHot() + " \n");
            stringBuffer.append(" \n");
        });
        // TODO 预测情况分析
        // 获取今日比赛
        List<WorldCupDetailDO> todayDetails = worldCupDetailService.getDetailListByTime(DateUtil.getDate());
        stringBuffer.append("---"  + " \n");
        stringBuffer.append("# 今日比赛情况" + " \n");
        int todayCount = todayDetails.size();
        stringBuffer.append("今天有" + todayCount + "场比赛" + " \n");
        todayDetails.forEach(e->{
            stringBuffer.append(e.getMatchName() + " " + e.getStartTime() + " \n");
            stringBuffer.append(e.getLeftName() + "-" + e.getRightName() + " ");
            stringBuffer.append(e.getLeftScore() + ":" + e.getRightScore() + " \n");
            stringBuffer.append("关注热度：" + e.getHot() + "\n");
            stringBuffer.append(" \n");
        });


        // 获取明日比赛
        List<WorldCupDetailDO> tomorrowDetails = worldCupDetailService.getDetailListByTime(DateUtil.getTomorrow());
        stringBuffer.append("---" + " \n");
        stringBuffer.append("# 明日比赛预告" + " \n");
        int tomorrowCount = todayDetails.size();
        stringBuffer.append("明天有" + tomorrowCount + "场比赛" + " \n");
        tomorrowDetails.forEach(e->{
            stringBuffer.append(e.getMatchName() + " " + e.getStartTime() + " \n");
            stringBuffer.append(e.getLeftName() + "-" + e.getRightName() + " ");
            stringBuffer.append(e.getLeftScore() + ":" + e.getRightScore() + " \n");
            stringBuffer.append("关注热度：" + e.getHot() + " \n");
            stringBuffer.append(" \n");
        });
        log.info(stringBuffer.toString());
        dingNotify.notifyMarkdown("2022世界杯", stringBuffer.toString());
    }

    /**
     * 实况比赛扫描，暂时不支持实况
     */
//    @Scheduled(cron = "10 0 0,3,18,21,23 * * *")
//    public void realtimeMatch(){
//        worldCupDetailService.initDetail();
//        // 只查询进行中的比赛
//        List<WorldCupDetailDO> list = worldCupDetailService.getList(new WorldCupDetailRo("1"));
//        if (list == null || list.size() == 0){
//            log.info("暂无进行中的比赛");
//        }else {
//            log.info("实况比赛扫描结果->{}", list.size(), list.get(0).getMatchName() + ":" + list.get(0).getLeftName() + "-" + list.get(0).getRightName());
//            list.forEach(e->{
//                String taskId = e.getMatchName() + e.getStartTime() + e.getLeftName() + e.getRightName() + "realtime";
//                // 每隔一分钟扫描一次比分情况
//                scheduledTaskCenter.addRealtimeMatchTaskMsgConfig(taskId, "0 */1 * * * ?", e);
//            });
//        }
//    }

}
